<?php

/**
 * @version 1.0
 * @created 08-Jun-2009 4:15:27 PM
 */
class UserManager
{
	
	function __construct()
	{
	}
	//all operations could be improved to use mem cache before hittin the DB
    
	public static function faultTest( User $usr ){
    	$dao = DAOFactory::getDAO();
		return $dao->faultTest( $usr );
    }
    
	public static function getBoardItems( User $usr, array $feddSources, $itemLimit = 3, $useCahce = true, $cacheLocation = '../cache' )
	{
		$dao = DAOFactory::getDAO();
		return $dao->getBoardItems( $feddSources, $itemLimit, $useCahce, $cacheLocation );
	}
	
	public static function getRoles( User $usr )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Admin"] ) ){
			return $roles;
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function getAllUsers( User $usr )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Site User"] ) ){
			$dao = DAOFactory::getDAO();
			return $dao->getAllUsers();
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function updateUser( User $usr )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Site User"] ) ){
			$dao = DAOFactory::getDAO();
			return $dao->updateUser( $usr );
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function updateRole( User $usr, Role $role )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Admin"] ) ){
			$dao = DAOFactory::getDAO();
			return $dao->updateRole( $role );
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function updateDomain( User $usr, Domain $domain )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Admin"] ) ){
			$dao = DAOFactory::getDAO();
			return $dao->updateDomain( $domain );
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function listDomains( User $usr )
	{
		$roles = SecurityManager::getAllRoles();
		if( SecurityManager::hasRole( $usr, $roles["Admin"] ) ){
			$dao = DAOFactory::getDAO();
			return $dao->listDomains();
		}else{
			throw new Exception('User does not have the required role to execute this operation');
		}
	}
	
	public static function loginUser( User $usr )
	{
		return SecurityManager::validateUserAccount($usr);
	}

}
?>